﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using FineUI;
using Newtonsoft.Json;
using ZHDJ.Core.Caching;
using ZHDJ.Data;
using ZHDJ.WebPage;
using TreeNode = System.Web.UI.WebControls.TreeNode;

namespace ZHDJ.Web.Portal
{
    public partial class CameraView : System.Web.UI.Page
    {
        public string Json
        {
            get { return Request["Json"] ?? ""; }
        }
        public string Search
        {
            get { return Request["Search"] ?? ""; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                LoadData();
            }
        }

        #region 数据操作

        private void LoadData()
        {
            if (Json != "" && Search == "")
            {
                BindTree();
            }
            else if (Json != "" && Search != "")
            {
                BindTreeBySearch();
            }
        }


        private void BindTree()
        {
            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName",
                true, CacheKeys.Cache_Branch_Tree_Navigation);
            //tree.EnableClickEvent = false;
            //tree.NavigateUrl = "../BranchIndex.aspx?BranchID=$nodeid";
            //tree.Target = "_blank";
            //tree.IconUrl = "/Resource/themes/default/images/Default/ytree.png";
            var TreeBranch = new FineUI.Tree();
            tree.BindTreeBranch(TreeBranch);
            List<TreeData> jqtree = new List<TreeData>();
            foreach (var item in TreeBranch.Nodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = 0 };
                jqtree.Add(data);
                CreateTreeData(item.Nodes, data);
            }
            var json = JsonConvert.SerializeObject(jqtree);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }

        /// <summary>
        /// 绑定查询
        /// </summary>
        private void BindTreeBySearch()
        {
            using (var DB = new DBInstance().DB)
            {
                List<TreeData> jqtree = new List<TreeData>();
                var data = DB.PartyOrgBranchs.Where(a => a.BranchName.Contains(Search)).OrderBy(a => a.BranchName).ToList();

                data.ForEach(a =>
                {
                    jqtree.Add(new TreeData() { id = a.BranchID, name = a.BranchName, level = 1 });
                });
                var json = JsonConvert.SerializeObject(jqtree);
                Response.Clear();
                Response.Write(json);
                Response.End();
            }

        }

        public void CreateTreeData(TreeNodeCollection treeNodes, TreeData treeData)
        {
            if (treeNodes == null)
            {
                return;
            }

            foreach (var item in treeNodes)
            {
                var data = new TreeData() { id = item.NodeID, name = item.Text, level = treeData.level + 1 };
                treeData.children.Add(data);
                CreateTreeData(item.Nodes, data);
            }

        }

        #endregion

        #region 事件


        #endregion
    }

    //public class TreeData
    //{
    //    public TreeData()
    //    {
    //        id = "";
    //        name = "";
    //        children = new List<TreeData>();
    //        level = 0;
    //    }

    //    public int level { get; set; }
    //    public string id { get; set; }
    //    public string name { get; set; }
    //    public List<TreeData> children { get; set; }
    //}
}
